<!--
 * @Description: 线图
 * @Version: 1.0
 * @Autor: gaoluo
 * @Date: 2021-09-10 17:54:19
 * @LastEditors: gaoluo
 * @LastEditTime: 2021-11-11 16:51:10
 * @FilePath: /nhw/src/components/Echarts/EchartsLine/index.vue
-->
<template>
  <div class="echarts-line-container">
    <div ref="container" v-show="tabName.length > 0" class="canvas-wrap"></div>
    <div v-show="tabName.length <= 0" class="error-msg">暂无数据</div>
  </div>
</template>

<script>
import echarts from "../index.js";
import { LineChart } from "echarts/charts";
echarts.use([LineChart]);

export default {
  name: "EchartsLine",

  components: {},
  props: {
    data: {
      type: Array,
      required: true,
    },
    smooth: {
      type: Boolean,
      default: true,
    },
    tabName: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      LineChart: null,
    };
  },
  watch: {
    data() {
      this.renderCharts();
    },
  },
  mounted() {
    const dom = this.$refs.container;
    this.LineChart = echarts.init(dom);
    this.renderCharts();
  },
  methods: {
    renderCharts() {
      this.LineChart.setOption({
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          top: "20%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          data: [...this.tabName],
          axisLabel: {
            interval: 0,
            fontSize: 12,
            rotate: 40,
          },
        },
        yAxis: {
          type: "value",
          name: "单位: 个",
          nameTextStyle: {
            fontSize: 12,
            lineHeight: 12,
          },
          axisLabel: {
            fontSize: 12,
          },
        },
        series: [
          {
            name: "",
            type: "line",
            smooth: this.smooth,
            data: [...this.data],
          },
        ],
      });
    },
  },
};
</script>

<style lang="less" scoped>
.echarts-line-container {
  width: 100%;
  .canvas-wrap {
    width: 100%;
    height: 400px;
  }
}

.error-msg {
  text-align: center;
  line-height: 1.9;
  color: #ccc;
}
</style>